package site.yunnong.atvris.common.utils;


import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class CopyUtils extends BeanUtils {

    public static <S,T> T copyObject(S source, Supplier<T> target){
        T t = target.get();
        try {

            copyProperties(t, source);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <S, T> List<T> copyList(List<S> sources, Supplier<T> target) {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            try {
                copyProperties(t, source);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            list.add(t);
        }
        return list;
    }
}
